package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.User;
import com.easy.common.service.IUserService;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import com.easy.common.util.PasswordUtil;

import java.time.LocalDateTime;
import java.util.List;

@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public User getById(Integer id) {
        return super.getOne(new QueryWrapper<User>().eq("id", id));
    }

    @Override
    public User getByUsername(String username) {
        return super.getOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public User getByPhone(String phone) {
        return super.getOne(new QueryWrapper<User>().eq("phone", phone));
    }

    @Override
    public User getByEmail(String email) {
        return super.getOne(new QueryWrapper<User>().eq("email", email));
    }

    @Override
    public int addUser(User user) {//添加用户不能加密
        user.setRole("用户");
        return super.save(user) ? 1 : 0;
    }

    @Override
    public int addAdmin(User user) {
        String password=user.getPassword();
        user.setPassword(PasswordUtil.getEncryption(password));
        user.setRole("管理员");
        return super.save(user) ? 1 : 0;
    }

    @Override
    public int updateUser(User user) {
        // 获取当前用户信息
        User currentUser = super.getById(user.getId());
        // 检查密码是否被修改
        if (currentUser != null && user.getPassword() != null && !user.getPassword().isEmpty()) {
            // 如果密码不同，说明需要修改密码，进行加密处理
            if (!user.getPassword().equals(currentUser.getPassword())) {
                String password = user.getPassword();
                user.setPassword(PasswordUtil.getEncryption(password));
            }
        }
        user.setUpdate_time(LocalDateTime.now());
        return super.updateById(user) ? 1 : 0;
    }

    @Override
    public int updateAdmin(User user) {
        // 获取当前用户信息
        User currentUser = super.getById(user.getId());
        // 检查密码是否被修改
        if (currentUser != null && user.getPassword() != null && !user.getPassword().isEmpty()) {
            // 如果密码不同，说明需要修改密码，进行加密处理
            if (!user.getPassword().equals(currentUser.getPassword())) {
                String password = user.getPassword();
                user.setPassword(PasswordUtil.getEncryption(password));
            }
        }
        user.setUpdate_time(LocalDateTime.now());
        return super.updateById(user) ? 1 : 0;
    }

    @Override
    public int deleteUser(Integer id) {
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public int deleteAdmin(Integer id) {
        //根据id删除，把is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public List<User> getUserAll(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getUsername, user.getUsername());
        queryWrapper.like(User::getPhone, user.getPhone());
        queryWrapper.eq(User::getRole,"用户");
        return super.list(queryWrapper);
    }

    @Override
    public List<User> getAdminAll(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(User::getUsername, user.getUsername());
        queryWrapper.like(User::getPhone, user.getPhone());
        queryWrapper.eq(User::getRole,"管理员");
        return super.list(queryWrapper);
    }

    @Override
    public IPage<User> getUserPage(User user, IPage<User> page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(user.getUsername() != null && !user.getUsername().isEmpty(), User::getUsername, user.getUsername());
        queryWrapper.like(user.getPhone() != null && !user.getPhone().isEmpty(), User::getPhone, user.getPhone());
        queryWrapper.eq(User::getIs_del, 0);
        queryWrapper.eq(User::getRole, "用户");
        return super.page(page, queryWrapper);
    }

    @Override
    public IPage<User> getAdminPage(User user, IPage<User> page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(user.getUsername() != null && !user.getUsername().isEmpty(), User::getUsername, user.getUsername());
        queryWrapper.like(user.getPhone() != null && !user.getPhone().isEmpty(), User::getPhone, user.getPhone());
        queryWrapper.eq(User::getIs_del, 0);
        queryWrapper.eq(User::getRole, "管理员");
        return super.page(page, queryWrapper);
    }

    //忘记密码时通过邮箱重置密码
    @Override
    public boolean resetPasswordByEmail(String email, String password) {
        // 通过邮箱查询用户
        User user = super.getOne(new QueryWrapper<User>().eq("email", email));
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        // 设置新密码并加密
        user.setPassword(PasswordUtil.getEncryption(password));
        // 更新用户信息
        super.updateById(user);
        return true;
    }

    // 用户自己在用户界面重置密码
    @Override
    public boolean resetPassword(String username, String oldPassword, String password) {
        // 通过用户名查询用户
        User user = super.getOne(new QueryWrapper<User>().eq("username", username));
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        // 验证原密码是否正确
        if (!PasswordUtil.checkPassword(oldPassword, user.getPassword())) {
            return false;
        }
        // 设置新密码并加密
        user.setPassword(PasswordUtil.getEncryption(password));
        // 更新用户信息
        super.updateById(user);
        return true;
    }
}
